package fightplan.algorithmen.ko;

import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumnModel;

import fightplan.business.AbstractFighter;
import fightplan.business.Fight;
import fightplan.business.FightEntry;
import fightplan.business.ResultHolder;
import fightplan.core.Style;

public class FinalrundePanel extends JPanel {

	private Fight fight;
	private FinalrundeTableModel tableModel;
	private JTable table;

	public FinalrundePanel(Fight fight) {
		super();
		this.fight = fight;

		tableModel = new FinalrundeTableModel();

		setLayout(new BorderLayout());

		table = new JTable(tableModel);
		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane, BorderLayout.CENTER);

		TableColumnModel cm = table.getColumnModel();
		cm.getColumn(0).setPreferredWidth(200);
		cm.getColumn(1).setPreferredWidth(60);
		cm.getColumn(2).setPreferredWidth(60);
		cm.getColumn(3).setPreferredWidth(60);
		cm.getColumn(4).setPreferredWidth(60);
		cm.getColumn(5).setPreferredWidth(60);
		cm.getColumn(6).setPreferredWidth(100);
		cm.getColumn(7).setPreferredWidth(100);

		scrollPane.getViewport().setBackground(Style.UNUSED_TABLE_SPACE);
	}

	public JTable getTable() {
		return table;
	}

	public class FinalrundeTableModel extends AbstractTableModel {

		@Override
		public String getColumnName(int column) {
			switch (column) {
			case 0:
				return "Name";
			case 6:
				return "Summe";
			case 7:
				return "Rang";
			default:
				return column + ".";
			}
		}

		public int getColumnCount() {
			return 8;
		}

		public int getRowCount() {
			return fight.getFighterCount();
		}

		public Object getValueAt(int rowIndex, int columnIndex) {

			switch (columnIndex) {
			case 0:
				AbstractFighter fighter = fight.getFighter(rowIndex).getFighter();
				return fighter != null ? fighter.getName() : null;
			case 6:
				FightEntry entry = fight.getFightEntries().get(rowIndex);
				Float f = entry.getOverallScore();
				return f != 0.0F ? f : "";
			case 7:
				fighter = fight.getFighter(rowIndex).getFighter();
				if (fighter == null)
					return null;

				for (int i = 0; i < fight.getFightEntries().size(); i++) {
					ResultHolder resultHolder = fight.getResultHolder(i);
					if (fighter.equals(resultHolder.getFighter())) {
						return (resultHolder.getRank() + 1) + ".";
					}
				}

				return null;
			default:
				entry = fight.getFightEntries().get(rowIndex);
				f = entry.getScore(columnIndex - 1);
				return f != 0.0F ? f : "";
			}
		}

		@Override
		public boolean isCellEditable(int rowIndex, int columnIndex) {
			return columnIndex >= 1 && columnIndex <= 5;
		}

		@Override
		public void setValueAt(Object value, int rowIndex, int columnIndex) {
			FightEntry entry = fight.getFightEntries().get(rowIndex);
			Float f = null;
			try {
				f =  Float.valueOf("" + value);
				entry.setScore(columnIndex - 1, f);
				fireTableDataChanged();
			} catch (NumberFormatException e) {
				// silent
			}
		}

	}

}
